﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Control_ucAutoComplete : controlMatrix //System.Web.UI.UserControl
{
    /// <summary>
    /// 指定要用那一個ASPX 來提供送資料的服務
    /// </summary>
    public string ServiceASPX
    {
        get;
        set;
    }
    /// <summary>
    /// 紀錄要另開視窗的檔名
    /// </summary>
    public string NewOpenPage
    {
        get;
        set;
    }

    /// <summary>
    /// 設定顯示AutoComplete 的寬度
    /// </summary>
    public int WindowWidth
    {
        get;
        set;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.LoadJquery(this, "jquery", "jquery");
        Utility.LoadJquery(this, "jqueryUI", "jqueryUI");
        Utility.LoadJquery(this, "autoComplete", "autoComplete");
        ScriptManager.RegisterClientScriptBlock(this, this.GetType()
            ,string.Format("AutoComplet_{0}",txtID.ClientID) , GetAutoCompleteScript(), true);
        
    }

    public string NewLabelID
    {
        get
        {
            return string.Format("newOpen_{0}", ClientID);
        }
    }


    public string GetAutoCompleteScript()
    {
        if (WindowWidth.Equals(0))
        {
            WindowWidth = 250;
        }
        string TableName = string.Empty;
        string enter = System.Environment.NewLine;
        return "$(function() {" + enter +
            "          $('#" + txtID.ClientID + "').autocomplete('" + ServiceASPX + "'," + enter +
            "            {" + enter +
            "                delay: 30," + enter +
            "                width: " + WindowWidth.ToString() + "," + enter +
            "                minChars: 1, //至少輸入幾個字元才開始給提示?" + enter +
            "                matchSubset: false," + enter +
            "                matchContains: false," + enter +
            "                cacheLength: 0," + enter +
            "                noCache: true, " + enter +
            "                onItemSelect: findValue," + enter +
            "                onFindValue: findValue," + enter +
            "                formatItem: function(row) {" + enter +
            "                    return \"<div style='height:12px'><div style='float:left'>\" + row[0] +" + enter +
            "                            \"</div>\" +" + enter +
            "                            \"<div style='float:right;padding-right:5px;'>\" +" + enter +
            "                            row[1] + \"</div>\";" + enter +
            "                }," + enter +
            "                extraParams: { t: '" + TableName + "' }, " + enter +
            "                autoFill: false," + enter +
            "                mustMatch: false " + enter +
            "            });" + enter +
            "              function findValue(li) {" + enter +
            "                  if (li == null) return alert('No match!');" + enter +
            "                  $('#" + txtID.ClientID + "').val(li.selectValue);" + enter +
            "                  $('#" + txtName.ClientID + "').val(li.extra[0]);" + enter +
            "                  $('#" + txtDisplay.ClientID + "').val(li.extra[0]);" + enter +
            "              }" + enter +
            "          });"            ;
    }

    public string GetAutoCompleteValue()
    {
        return txtID.Text;
    }

    public void SetLabelClick()
    {
        //沒有給要另開視窗的檔案名，就不進行Script 的設定
        if (NewOpenPage == null)
        {
            return;
        }
        Utility.LoadJquery(this, "jquery", "jquery");
        string lblClick = @"$(document).ready(function()
{
	$('#label1').bind('click', function()
	{
		var frm = window.open('ASPX?hdn=Cloudio'
		, '_blank'
		, 'height=200, menubar=0, resizable=1, scrollbars=1, status=0, width=200');
        
	});
});
";
        lblClick = lblClick.Replace("ASPX", NewOpenPage);
        lblClick = lblClick.Replace("label1", NewLabelID);
        lblClick = lblClick.Replace("Cloudio", txtID.ClientID);
        ScriptManager.RegisterClientScriptBlock(this, this.GetType()
         , NewLabelID, lblClick, true);
    }
}
